package com.yoomiito.app.ui.fragment.cicle

import android.os.Bundle
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import com.yoomiito.app.R
import com.yoomiito.app.base.App
import com.yoomiito.app.base.BaseLazyFragment
import com.yoomiito.app.model.BaseList
import com.yoomiito.app.model.MaterialContent
import com.yoomiito.app.model.MaterialLabel
import com.yoomiito.app.ui.anyview.material.MaterialView
import com.yoomiito.app.utils.DensityUtil
import com.yoomiito.app.utils.EventMessage
import com.yoomiito.app.utils.Type
import com.yoomiito.app.widget.state.ContentType
import com.yoomiito.app.widget.state.EmptyType
import com.yoomiito.app.widget.state.StatusManager
import kotlinx.android.synthetic.main.fm_new_material.*

/**
 * 宣传素材
 */
class NewMaterialFragment : BaseLazyFragment<NewMaterialPresenter>() {

    private var materialView: MaterialView? = null
    private var mCurrentPage = 1
    private var mCurrentId: Long = 0
    override fun getLayoutId(): Int = R.layout.fm_new_material

    override fun newP(): NewMaterialPresenter = NewMaterialPresenter(App.mAppConfig)

    override fun initData_kotlin(view: View?, savedInstanceState: Bundle?) {
        super.initData_kotlin(view, savedInstanceState)
        materialView = MaterialView(context)
        materialView?.OnRefreshListener = { type, id ->
            when (type) {
                1 -> {//下拉
                    mCurrentPage = 1
                }
                2 -> {//上拉
                    mCurrentPage++
                }
                3 -> {//点击tab
                    mCurrentPage = 1
                    mCurrentId = id
                    showLoading()
                }
            }
            requestList()
        }
        mStatusManager = StatusManager.Builder(context)
                .addType(ContentType(materialView))
                .addType(EmptyType(R.layout.item_empty_1).setShowViewListener {
                    val iv = it.findViewById<ImageView>(R.id.bg_empty)
                    val lp = iv.layoutParams as LinearLayout.LayoutParams
                    lp.topMargin = DensityUtil.dp2px(123f)
                })
                .build()
        frameLayout.addView(mStatusManager.view())
        requestLabel()
    }

    private fun requestLabel() {
        showLoading()
        mCurrentPage = 1
        mCurrentId = 0
        p.getLabel()
    }

    private fun requestList() {
        p.getList(mCurrentId, mCurrentPage)
    }

    fun showLabel(labels: MutableList<MaterialLabel>?) {
        val labels = labels?: mutableListOf<MaterialLabel>()
        labels?.add(0, MaterialLabel(0,"全部"))
        materialView?.showLabel(labels)
        requestList()
    }

    fun showList(datas: BaseList<MaterialContent>?) {
        mCurrentPage = datas?.current_page!!
        materialView?.showList(datas)
        dismissLoading()
    }

    override fun eventMessage(eventMessage: EventMessage?) {
        super.eventMessage(eventMessage)

        if (Type.EventMsgType.LoginOut == eventMessage?.eventType || Type.EventMsgType.LoginSuccess == eventMessage?.eventType) {
            requestLabel()
        }
    }
}